import hashlib
from django.db import models

# Create your models here.
class Creater(models.Model):
    name = models.CharField(max_length=200,null=True,verbose_name='用户名')
    password = models.CharField(max_length=200,null=True,verbose_name='密码')
    token = models.CharField(max_length=200,default='',blank=True,verbose_name='token')
    profile_pic = models.CharField(max_length=200,null=True,verbose_name='头像')
    class Meta:
        verbose_name = '用户'
        verbose_name_plural = verbose_name

    def __unicode__(self):
        return self.name

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        self.password = hashlib.sha1(self.password.encode('utf-8')).hexdigest()
        super(Creater,self).save(*args, **kwargs)

    def natural_key(self):
        return self.__str__(),self.profile_pic

# 分类，用作别的表的外键
class Category(models.Model):
    name = models.CharField(max_length=200,null=True,verbose_name='名称')

    class Meta:
        verbose_name = '类别'
        verbose_name_plural = verbose_name
    
    def __unicode__(self):
        return self.name
    
    def __str__(self):
        return self.name

    def natural_key(self):
        return self.__str__()

# 文章
class Article(models.Model):
    category = models.ForeignKey(Category, null=True,on_delete=models.SET_NULL,verbose_name='类别')
    name = models.ForeignKey(Creater, null=True,on_delete=models.SET_NULL,verbose_name='作者')
    date_created = models.DateTimeField(auto_now_add=True, null=True,verbose_name='创建时间')
    title = models.CharField(max_length=200,verbose_name='标题')
    content = models.CharField(max_length=10000,verbose_name='内容')
    like = models.CharField(max_length=200,default='1',verbose_name='Like')
    cover_image = models.CharField(max_length=200,null=True,verbose_name='封面图')
    class Meta:
        verbose_name = '文章'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return self.title

    def natural_key(self):
        return self.__str__(),self.cover_image,self.pk

class Comment(models.Model):
    article = models.ForeignKey(Article, null=True,on_delete=models.CASCADE,verbose_name='文章')
    name = models.ForeignKey(Creater, null=True,on_delete=models.SET_NULL,verbose_name='作者')
    date_created = models.DateTimeField(auto_now_add=True, null=True,verbose_name='创建时间')
    content = models.CharField(max_length=100,verbose_name='内容')
    class Meta:
        verbose_name = '评论'
        verbose_name_plural = verbose_name
    
    def __str__(self):
        return self.content

class Follow(models.Model):
    follow_name = models.ForeignKey(Creater, null=True,on_delete=models.SET_NULL,verbose_name='作者')
    name = models.CharField(max_length=200,null=True,verbose_name='用户名')
    class Meta:
        verbose_name = '关注列表'
        verbose_name_plural = verbose_name
    def __str__(self):
        return self.name

class Images(models.Model):
    image = models.ImageField(null=True,blank=True,verbose_name='图片')
    class Meta:
        verbose_name = '图片'
        verbose_name_plural = verbose_name